package com.syq.service.impl;


import com.syq.dao.CourseTemplateDetailsDao;
import com.syq.dao.RedisDao;
import com.syq.dto.DTO;
import com.syq.dto.MessageEnum;
import com.syq.entity.Account;
import com.syq.entity.CourseTemplateDetails;
import com.syq.page.PageInfo;
import com.syq.service.AccountService;
import com.syq.service.CourseTemplateDetailsService;
import com.syq.service.MailSendService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Optional;

@Service("courseTemplateDetailsService")
public class CourseTemplateDetailsServiceImpl implements CourseTemplateDetailsService, AccountService {
    @Resource
    CourseTemplateDetailsDao courseTemplateDetailsDao;
    @Resource
    MailSendService mailSendService;
    @Resource
    RedisDao redisDao;
    @Override
    public DTO<CourseTemplateDetails> getAllCourseTemplateDetailss()  throws Exception{
        List<CourseTemplateDetails> courseTemplateDetailss = courseTemplateDetailsDao.getAllCourseTemplateDetailss();
        MessageEnum messageEnum  = Optional.ofNullable(courseTemplateDetailss).isPresent()?MessageEnum.GETPARENTSSUCCESS: MessageEnum.GETPARENTSFAIL;
        return new DTO<CourseTemplateDetails>(messageEnum.getCode(),messageEnum.getMessage(),courseTemplateDetailss);
    }

    @Override
    public DTO<CourseTemplateDetails> deleteCourseTemplateDetailsByCotTno(String cotTno)  throws Exception{
        Integer result = courseTemplateDetailsDao.deleteCourseTemplateDetailsByCotTno(cotTno);
        MessageEnum messageEnum  =result> 0?MessageEnum.DELETEPARENTSSUCCESS:MessageEnum.DELETEPARENTSFAIL;
        return new DTO<CourseTemplateDetails>(messageEnum.getCode(),messageEnum.getMessage());
    }

    @Override
    public DTO<CourseTemplateDetails> updateCourseTemplateDetailsByCotTno(CourseTemplateDetails courseTemplateDetails)  throws Exception{
        Integer result = courseTemplateDetailsDao.updateCourseTemplateDetailsByCotTno(courseTemplateDetails);
        MessageEnum messageEnum  =result> 0?MessageEnum.UPDATEPARENTSSUCCESS:MessageEnum.UPDATEPARENTSFAIL;
        return new DTO<CourseTemplateDetails>(messageEnum.getCode(),messageEnum.getMessage());
    }

    @Override
    public DTO<CourseTemplateDetails> addCourseTemplateDetails(CourseTemplateDetails courseTemplateDetails) throws Exception {
         //courseTemplateDetails.setPstate("未激活");
        Integer result = courseTemplateDetailsDao.addCourseTemplateDetails(courseTemplateDetails);
        MessageEnum messageEnum  = result> 0?MessageEnum.ADDPARENTSSUCCESS:MessageEnum.ADDPARENTSFAIL;
        return new DTO<CourseTemplateDetails>(messageEnum.getCode(),messageEnum.getMessage(),courseTemplateDetails);
    }

    @Override
    public DTO<CourseTemplateDetails> getCourseTemplateDetailsByPage(PageInfo pageInfo ) throws Exception {
        List<CourseTemplateDetails> courseTemplateDetailss = courseTemplateDetailsDao.getCourseTemplateDetailsByPage(pageInfo);
        MessageEnum messageEnum  = Optional.ofNullable(courseTemplateDetailss).isPresent()?MessageEnum.ADDADMINSUCCESS: MessageEnum.GETPARENTSFAIL;
        return new DTO<CourseTemplateDetails>(messageEnum.getCode(),messageEnum.getMessage(),courseTemplateDetailss,pageInfo);
    }

//    @Override
//    public DTO<CourseTemplateDetails> selectCourseTemplateDetailsWithChildren(String cotTno)  throws Exception{
//        CourseTemplateDetails courseTemplateDetails = courseTemplateDetailsDao.selectCourseTemplateDetailsWithChildren(cotTno);
//        MessageEnum messageEnum  = Optional.ofNullable(cotTno).isPresent()?MessageEnum.GETPARENTSSUCCESS:MessageEnum.GETPARENTSFAIL;
//        return new DTO<CourseTemplateDetails>(messageEnum.getCode(),messageEnum.getMessage(),courseTemplateDetails);
//    }
//
//    @Override
//    public DTO<CourseTemplateDetails> getCourseTemplateDetailsWithOrdrees(String cotTno) throws Exception {
//        CourseTemplateDetails courseTemplateDetails = courseTemplateDetailsDao.getCourseTemplateDetailsWithOrdrees(cotTno);
//        MessageEnum messageEnum  = Optional.ofNullable(cotTno).isPresent()?MessageEnum.GETPARENTSSUCCESS:MessageEnum.GETPARENTSFAIL;
//        return new DTO<CourseTemplateDetails>(messageEnum.getCode(),messageEnum.getMessage(),courseTemplateDetails);
//    }
//
//    @Override
//    public DTO<CourseTemplateDetails> sendCodeMail(String cname) throws Exception {
//        List<CourseTemplateDetails> courseTemplateDetailss = courseTemplateDetailsDao.getCourseTemplateDetailsByExample(cname);
//        DTO<CourseTemplateDetails> dto = null;
//        MessageEnum messageEnum  = null;
//       if(!Optional.ofNullable(courseTemplateDetailss).isPresent()){
//           messageEnum = Optional.ofNullable(courseTemplateDetailss).isPresent()?MessageEnum.GETPARENTSBYEXAMPLESUCCESS:MessageEnum.GETPARENTSBYEXAMPLEFAIL;
//       }else{
//           String code = GenNumTools.addZero(4,(Math.abs(new Random().nextInt()%1000)));
//           redisDao.set(courseTemplateDetailss.get(0).getCotTno(),code);
//           String[] mail = MailMessageUtils.getMessage(courseTemplateDetailss.get(0),"CourseTemplateDetails:sendCode;"+code);
//           mailSendService.sendMail(courseTemplateDetailss.get(0),mail[0],mail[1]);
//           messageEnum = MessageEnum.ADDACTIVITYPESUCCESS;
//       }
//        return new DTO<CourseTemplateDetails>(messageEnum.getCode(),messageEnum.getMessage(), courseTemplateDetailss);
//    }
//
//    @Override
//    public DTO<CourseTemplateDetails> updateCourseTemplateDetailsByPwd(CourseTemplateDetailsVo courseTemplateDetailsVo) throws Exception {
//        String code = redisDao.get(courseTemplateDetailsVo.getCotTno());
//        if(!courseTemplateDetailsVo.getCode().equals(code)){
//            return new DTO<>(401,"验证码不正确");
//        }
//        CourseTemplateDetails courseTemplateDetails = new CourseTemplateDetails();
//        courseTemplateDetails.setCotTno(courseTemplateDetailsVo.getCotTno());
//        courseTemplateDetails.setCname(courseTemplateDetailsVo.getUsername());
//        String algorithmName = "md5";
////        String courseTemplateDetailsname = courseTemplateDetailsVo.getPrentsname();
//        String courseTemplateDetailsname = courseTemplateDetailsVo.getUsername();
//        String password = courseTemplateDetailsVo.getPassword();
//        String salt1 = courseTemplateDetailsname;
//        String salt2 = new SecureRandomNumberGenerator().nextBytes().toHex();
//        int hashIterations = 3;
//        SimpleHash hash = new SimpleHash(algorithmName, password, salt1 + salt2, hashIterations);//参数1选择md5加密方式，参数2 原始密码  参数3 一个特定值（salt1 用户名 ）
//        // +salt 2随机值 作为加工的佐料，参数4 佐料加的次数
//        String encodedPassword = hash.toHex();//hash出来的是128位的2进制方便存储转换为32为的字符串
//        System.out.println("000000000000000000000000");
//        System.out.println(encodedPassword);
//        courseTemplateDetails.setPpwd(encodedPassword);
//        courseTemplateDetailsDao.updateCourseTemplateDetailsByCotTno(courseTemplateDetails);
//        MessageEnum messageEnum  = Optional.ofNullable(courseTemplateDetailsVo).isPresent()?MessageEnum.GETPARENTSSUCCESS:MessageEnum.GETPARENTSFAIL;
//        return new DTO<CourseTemplateDetails>(messageEnum.getCode(),messageEnum.getMessage(),courseTemplateDetails);
//    }
//
    @Override
    public DTO<Account> getAccountByAccountName(String accountName) throws Exception {
//        CourseTemplateDetails courseTemplateDetails = courseTemplateDetailsDao.getAccountByAccountName(accountName);
//        System.out.println(courseTemplateDetails+"--------666666---------");
//        MessageEnum messageEnum  = Optional.ofNullable(accountName).isPresent()?MessageEnum.GETPARENTSSUCCESS:MessageEnum.GETPARENTSFAIL;
//        return new DTO<Account>(messageEnum.getCode(),messageEnum.getMessage(),courseTemplateDetails);
        return null;
    }
//
//
//
//    @Override
//    public DTO<CourseTemplateDetails> activate(String cotTno) throws Exception {
//        CourseTemplateDetails courseTemplateDetails = new CourseTemplateDetails();
//        courseTemplateDetails.setCotTno(cotTno);
//        courseTemplateDetails.setPstate("已激活");
//        Integer result = courseTemplateDetailsDao.updateByPrimaryKeySelective(courseTemplateDetails);
//        //isPresent表示判断不为空成立则dto为？与：之间的对象
//        MessageEnum messageEnum = result > 0 ? MessageEnum.UPDATEADMINSUCCESS : MessageEnum.UPDATEADMINFAIL;
//        return new DTO<CourseTemplateDetails>(messageEnum.getCode(), messageEnum.getMessage());
//    }



}
